<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>添加门店 - 766 SMART HOME</title>
<meta name="author" content="BIZ Team" />
<meta name="copyright" content="BIZ" />
<link href="${base}/resources/admin/css/common.css" rel="stylesheet" type="text/css" />
<style type="text/css">
	#ranges{height:100px;width:1200px}
	#mapContainer{height:400px;width:100%;}
</style>
</head>
<body>
	<div class="path">
		<a href="${base}/admin/common/index.jhtml">${message("admin.path.index")}</a> &raquo;添加门店
	</div>
	<form id="inputForm" action="add.jhtml" method="post">
		<ul id="tab" class="tab">
			<li>
				<input type="button" value="门店基本信息" />
			</li>
		</ul>
		<table class="input tabContent">
			<tr>
				<th>
					<span class="requiredField">*</span>门店名称:
				</th>
				<td>
					<input type="text" name="shopsName" class="text" maxlength="200" value=""/>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>门店地址:
				</th>
				<td>
					<input type="text" name="address" class="text storeAddress" maxlength="200" value=""/>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>联系人:
				</th>
				<td>
					<input type="text" name="contactP" class="text" value="" />
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>联系人电话:
				</th>
				<td>
					<input type="text" name="contactT" class="text" value=""/>
				</td>
			</tr>
			<tr id="storeTypeTemp">
				<th>
					<span class="requiredField">*</span>门店类型:
				</th>
				<td>
					 <select class="option" name="storeType">
					 <option value="">--请选择--</option>
					 [#list shopsTypeList as shopsType]
					 	<option value="${shopsType.id}">${shopsType.title}</option>
					 [/#list]
					</select>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>门店性质:
				</th>
				<td>
					<input type="checkbox" checked="checked" name="orderShops" value="0"/><span>下单门店</span><input type="checkbox" checked="checked" name="serviceShops" value="1"/><span>服务门店</span>
					<input class="hidden" name="tempShopsNature" id="tempShopsNature" value="0"/>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>纳税人类别:
				</th>
				<td>
					<select class="option" name="taxpayerType">
					<option value="">--请选择--</option>
					 [#list taxpayerType as taxpayerType]
					 	<option value="${taxpayerType.id}">${taxpayerType.title}</option>
					 [/#list]
					</select>
				</td>
			</tr>
			<tr>
				<th>
					<span class="requiredField">*</span>门店规模:
				</th>
				<td>
					 <select class="option" name="storeScale">
					 <option value="">--请选择--</option>
					 [#list storeScale as storeScale]
					 	<option value="${storeScale.id}">${storeScale.title}</option>
					 [/#list]
					 </select>
				</td>
			</tr>
			<tr id="shopsArea">
				<th>
					<span class="requiredField">*</span>门店区域:
				</th>
				<td class="parent">
				 <select class="option" name="provinceId">
				 	<option value="">--请选择--</option>
					[#list parentList as li]
						<option value="${li.id}">${li.name}</option>
				 	[/#list]
				 </select>
				 <select class="option selectedCity" id="selectCity" name="cityId">
				 	<option value="">--请选择--</option>
				 </select>
				 <select class="option" name="areaId">
				 	<option value="">--请选择--</option>
				 </select>
				</td>
			</tr>
			<tr id="shopsLV">
				<th>
					<span class="requiredField">*</span>门店等级:
				</th>
				<td>
					 <select class="option" name="shopsLevel">
					 <option value="">--请选择--</option>
					 [#list shopsLV as shopsLV]
					 	<option value="${shopsLV.id}">${shopsLV.title}</option>
					 [/#list]
					</select>
				</td>
			</tr>
			<tr id="serviceArea">
				<th>
					<span class="requiredField">*</span>服务区域:
				</th>
				<td class="parents">
				 <select class="option" name="saProvinceId">
				 	<option value="">--请选择--</option>
					[#list parentList as li]
						<option value="${li.id}">${li.name}</option>
				 	[/#list]
				 </select>
				 <select class="option selectedCity" name="saCityId" id="saCityId">
				 	<option value="">--请选择--</option>
				 </select>
				 <select class="option selectedCity" name="saAreaId" id="saAreaId">
				 	<option value="">--请选择--</option>
				 </select>
				</td>
			</tr>
			<tr id="selects">
				<th>
					<span class="requiredField"></span>可选多个
				</th>
				<td id="addArea">
				</td>
			</tr>
			<tr id="sad" class="">
				<th>
					<span class="requiredField">*</span>门店具体地址:
				</th>
				<td>
				 	<input type="text" class="text" id="addressText" name="gpsAddress"/>
				 	<input type="button" class="button" value="查询" id="checkAddress"/>
				 	<label class="fieldError check_address_prompt hidden">您输入的地址没有解析到结果!</label>
				</td>
			</tr>
			<tr id="position" class="">
				<th>
					<span class="requiredField">*</span>门店位置:
				</th>
				<td id="gps">
					<div id="mapContainer"></div>
					<input type="hidden" name="gpsX" class="text coordinateX" id="coordinateX" value=""/>
					<input type="hidden" name="gpsY" class="text coordinateY" id="coordinateY" value=""/>
				</td>
			 </tr>
			<tr>
				<th>
					<span class="requiredField"></span>门店描述:
				</th>
				<td>
					<textarea name="ranges" id="ranges"></textarea>
				</td>
			 </tr>
		</table>
		<table class="input">
			<tr>
				<th>
					&nbsp;
				</th>
				<td>
					<input type="submit" class="button" value="${message("admin.common.submit")}" />
					<input type="button" class="button" value="${message("admin.common.back")}" onclick="location.href='list.jhtml'" />
				</td>
			</tr>
		</table>
	</form>
</body>
<script type="text/javascript" src="${base}/resources/admin/js/jquery.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/common.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/jquery.tools.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/jquery.validate.js"></script>
<script type="text/javascript" src="${base}/resources/admin/editor/kindeditor.js"></script>
<script type="text/javascript" src="${base}/resources/admin/js/input.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=494PEuZs8md8xKnsIvrYYmvq"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css">
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css">
<script type="text/javascript" src="${base}/resources/admin/js/locationMap.js"></script>
<script type="text/javascript">
$().ready(function() {
	[@flash_message /]
	var $inputForm = $("#inputForm");
	// 表单验证
	$inputForm.validate({
		rules: {
			shopsName:{
				required: true,
				maxlength:20
			},
			address:{
				required: true,
				maxlength:100
			},
			contactP:{
				required: true,
				maxlength:10
			},
			contactT:{
				required: true,
				maxlength:13
			},
			storeType:{
				required: true
			},
			taxpayerType:{
				required: true
			},
			storeScale:{
				required: true
			},
			provinceId:{
				required: true
			},
			cityId:{
				required: true
			},
			areaId:{
				required: true
			},
			gpsX:{
				required: true
			},
			saProvinceId:{
				required: true
			},
			saAreaId:{
				required: true
			},
			saCityId:{
				required: true
			},
			tempShopsNature:{
				required: true
			},
			shopsLevelA:{
				required: true
			},
			shopsLevel:{
				required: true
			}
		},
		messages: {
			shopsName: {
				required: "必填",
				maxlength: "最多输入20个字符"
			},
			address:{
				required: "必填",
				maxlength: "最多输入100个字符"
			},
			contactP:{
				required: "必填",
				maxlength: "最多输入10个字符"
			},
			contactT:{
				required: "必填",
				maxlength: "最多输入13个字符"
			},
			storeType:{
				required: "必填"
			},
			taxpayerType:{
				required: "必填"
			},
			storeScale:{
				required: "必填"
			},
			provinceId:{
				required: "必填"
			},
			cityId:{
				required: "必填"
			},
			areaId:{
				required: "必填"
			},
			gpsX:{
				required: "请在地图上选择位置"
			},
			saProvinceId:{
				required: "必填"
			},
			saAreaId:{
				required: "必填"
			},
			saCityId:{
				required: "必填"
			},
			tempShopsNature:{
				required: "请选择门店性质"
			},
			shopsLevelA:{
				required: "请选A级门店"
			},
			shopsLevel:{
				required: "请选门店等级"
			}
		}
	});
	$("[name=provinceId]").change(function(){
		//清空等级
		//默认选择请选择
		$("[name=shopsLevel] option:first").prop("selected","selected");
		$("#shopsLevelChirld").remove();
		var provinceId = $(this).val();
		$.ajax({
		    url:"${base}/admin/shops/getChildrenByParentId.jhtml",    //请求的url地址
		    dataType:"json",   //返回格式为json
		    data:{"provinceId":provinceId},    //参数值
		    type:"GET",   //请求方式
		    success:function(req){
		        //请求成功时处理
		        $("[name=cityId]").empty();
		        $("[name=cityId]").append("<option value=''>--请选择--</option>");
		        for(var i=0;i<req.length;i++){
		        	$("[name=cityId]").append("<option value='"+req[i].id+"'"+"info='"+req[i].fullName+"'>"+req[i].name+"</option>");
		        }
		    },
		    error:function(){
		        //请求出错处理
		    }
		});
	});
	$("[name=cityId]").change(function(){
		var provinceId = $(this).val();
		$.ajax({
		    url:"${base}/admin/shops/getChildrenByParentId.jhtml",    //请求的url地址
		    dataType:"json",   //返回格式为json
		    data:{"provinceId":provinceId},    //参数值
		    type:"GET",   //请求方式
		    success:function(req){
		        //请求成功时处理
		        $("[name=areaId]").empty();
		        $("[name=areaId]").append("<option value=''>--请选择--</option>");
		        if(req.length<1){
		        	$("[name=areaId]").remove();
		        }
		        if(req.length>0){
		        	$("[name=areaId]").remove();
		        	$(".parent").append("<select class='option' name='areaId'><option value=''>--请选择--</option></select>");
		        }
		        for(var i=0;i<req.length;i++){
		        	$("[name=areaId]").append("<option value='"+req[i].id+"'"+"info='"+req[i].fullName+"'>"+req[i].name+"</option>");
		        }
		    },
		    error:function(){
		        //请求出错处理
		    }
		});
	});
	//服务区域
	$("[name=saProvinceId]").change(function(){
		var provinceId = $(this).val();
		var provinceName = $(this).find("option:selected").text();
		$.ajax({
		    url:"${base}/admin/shops/getChildrenByParentId.jhtml",    //请求的url地址
		    dataType:"json",   //返回格式为json
		    data:{"provinceId":provinceId},    //参数值
		    type:"GET",   //请求方式
		    success:function(req){
		        //请求成功时处理
		        $("[name=saCityId]").empty();
		        $("[name=saCityId]").append("<option value=''>--请选择--</option>");
		        if(provinceName.indexOf("市")>=0){
		        	$("[name=saCityId]").append("<option value='0'>--全选--</option>");
		        }        
		        for(var i=0;i<req.length;i++){
		        	$("[name=saCityId]").append("<option value='"+req[i].id+"'"+"info='"+req[i].fullName+"'>"+req[i].name+"</option>");
		        }
		        $("#addArea").children().remove();
		    },
		    error:function(){
		        //请求出错处理
		    }
		});
	});
	$("[name=saCityId]").change(function(){
		var provinceName = $("[name=saProvinceId]").find("option:selected").text();
		if(provinceName.indexOf("市")<0){
			$("#addArea").children().remove();
		}
		var provinceId = $(this).val();
		var cityName=$("#saCityId").find("option:selected").text();
		$.ajax({
		    url:"${base}/admin/shops/getChildrenByParentId.jhtml",    //请求的url地址
		    dataType:"json",   //返回格式为json
		    data:{"provinceId":provinceId},    //参数值
		    type:"GET",   //请求方式
		    success:function(req){
		        //请求成功时处理
		        $("#saAreaId").empty();
		        $("#saAreaId").append("<option value=''>--请选择--</option>");
		        if(req.length<1){
		        	$("[name=saAreaId]").remove();
					var tempName = $("#saCityId").find("option:selected").text();
					var tempValue = $("#saCityId").find("option:selected").val();
		        	var serviceId  = $("[name=tempId]");
		        	//去掉重复的选择和请选择
					if(tempValue.length<1){
						$("#addArea").children().remove();
						return;
					}else{
						if(serviceId.length>0){
							var str="";
							for(var i=0;i<serviceId.length;i++){
								if(tempValue==serviceId[i].value){
									return;
								}
							}
						}
					}
					//全选
					if(provinceId==0){
						$("#addArea").children().remove();
						var len = $("#saCityId option").length;
						for(var j=2;j<len;j++){
							var text = $("#saCityId option:eq("+j+")").val();
							var value = $("#saCityId option:eq("+j+")").text();
							$("#addArea").append("<span><input class='hidden' name='tempId' value='"+text+"'/><a href='javascript:;' name='tempName'>["+value+"]</a></span>");
						}
						return;
					}
		        	$("#addArea").append("<span><input class='hidden' name='tempId' value='"+provinceId+"'/><a href='javascript:;' name='tempName'>["+cityName+"]</a></span>");
		        }
		        if(req.length>0){
		        	$("[name=saAreaId]").remove();
		        	$(".parents").append("<select class='option' name='saAreaId' id='saAreaId'><option value=''>--请选择--</option><option value='0'>--全选--</option></select>");
		        }
		        for(var i=0;i<req.length;i++){
		        	$("[name=saAreaId]").append("<option value='"+req[i].id+"'"+"info='"+req[i].fullName+"'>"+req[i].name+"</option>");
		        }
		    },
		    error:function(){
		        //请求出错处理
		    }
		});
	});
	$("[name=saAreaId]").live("change",function(){
		var tempId = $(this).val();
		var tempName = $("#saAreaId").find("option:selected").text();
		var tempValue = $("#saAreaId").find("option:selected").val();
		var serviceId  = $("[name=tempId]");
		//去掉重复的选择和请选择
		if(tempValue.length<1){
			return;
		}else{
			if(serviceId.length>0){
				var str="";
				for(var i=0;i<serviceId.length;i++){
					if(tempValue==serviceId[i].value){
						return;
					}
				}
			}
		}
		//全选
		if(tempId==0){
			$("#addArea").children().remove();
			var len = $("#saAreaId option").length;
			for(var j=2;j<len;j++){
				var text = $("#saAreaId option:eq("+j+")").val();
				var value = $("#saAreaId option:eq("+j+")").text();
				$("#addArea").append("<span><input class='hidden' name='tempId' value='"+text+"'/><a href='javascript:;' name='tempName'>["+value+"]</a></span>");
			}
			return;
		}
		//$("#addArea").children().remove();
		$("#addArea").append("<span><input class='hidden' name='tempId' value='"+tempId+"'/><a href='javascript:;' name='tempName'>["+tempName+"]</a></span>");
	});
	$("[name=tempName]").live("click",function(){
		$(this).prev().remove();
		$(this).remove();
	});
	$("[name=storeType]").change(function(){
		var storeType = $(this).val();
		if(storeType==43){
			$("#sad").attr("class","hidden");
			$("#position").attr("class","hidden");
			$("[name=gpsX]").remove();
			$("[name=gpsY]").remove();
		}else{
			$("#sad").attr("class","");
			$("#position").attr("class","");
			$("[name=gpsX]").remove();
			$("[name=gpsY]").remove();
			var content = "<input type='hidden' name='gpsX' class='text coordinateX' id='coordinateX' value=''/><input type='hidden' name='gpsY' class='text coordinateY' id='coordinateY' value=''/>";
			$("#gps").append(content);
		}
	});
	$("[name=orderShops]").click(function(){
		var orderShops = $(this).attr("checked");
		var serviceShops = $("[name=serviceShops]").attr("checked");
		if(serviceShops!="checked" && orderShops!="checked"){
			$("[name=tempShopsNature]").attr("value","");
		}else{
			$("[name=tempShopsNature]").attr("value","0");
		}
	});
	//服务门店
	$("[name=serviceShops]").click(function(){
		var serviceShops = $(this).attr("checked");
		var orderShops = $("[name=orderShops]").attr("checked");
		if(serviceShops!="checked" && orderShops!="checked"){
			$("[name=tempShopsNature]").attr("value","");
		}else{
			$("[name=tempShopsNature]").attr("value","0");
		}
		if(serviceShops=="checked"){
			setShow();
		}else{
			setHidden();
		}
	});
	$("[name=shopsName]").blur(function(){
		var tempShopsName = $(this).val();
		var totalShops = '${totalShops.shopsName}';
		if(totalShops==tempShopsName){
			$.message("error","总部售后只能添加一次,请检查门店名称");
			$(this).val("");
			return;
		}
		//总部售后不需要填写服务区域和地图位置
		if(tempShopsName=='总部售后'){
			//隐藏掉门店类型
			$("#storeTypeTemp").attr("class","hidden");
			$("[name=storeType] option:first").prop("selected","selected");
			$("[name=storeType] option:first").attr("value","00");
			//隐藏点服务区域
			setHidden();
			//隐藏掉地图
			$("#sad").attr("class","hidden");
			$("#position").attr("class","hidden");
			$("[name=gpsX]").remove();
			$("[name=gpsY]").remove();
			//门店性质复选框设置
			$("[name=orderShops]").attr("disabled","disabled");
			$("[name=orderShops]").removeAttr("checked");
			$("[name=serviceShops]").attr({disabled:'disabled',checked:'checked'});
			//默认A级门店
			$("[name=shopsLevel] option:eq(1)").prop("selected","selected");
			$("[name=shopsLevel]").attr("disabled","disabled");
			
		}else{
			$("#storeTypeTemp").attr("class","");
			//显示服务区域
			setShow();
			//显示地图
			$("#sad").attr("class","");
			$("#position").attr("class","");
			$("[name=gpsX]").remove();
			$("[name=gpsY]").remove();
			var content = "<input type='hidden' name='gpsX' class='text coordinateX' id='coordinateX' value=''/><input type='hidden' name='gpsY' class='text coordinateY' id='coordinateY' value=''/>";
			$("#gps").append(content);
			$("[name=shopsLevel]").removeAttr("disabled");
			$("[name=orderShops]").removeAttr("disabled");
			$("[name=serviceShops]").removeAttr("disabled");
		}
	});
	function setHidden(){
			$("#serviceArea").attr("class","hidden");
			$("#selects").attr("class","hidden");
			$("[name=saProvinceId] option:first").prop("selected","selected");
			$("[name=saProvinceId] option:first").attr("value","00");
			
			$("[name=saCityId] option:first").prop("selected", 'selected');
			$("[name=saCityId] option:first").attr("value", '00');
			
			$("[name=saAreaId] option:first").prop("selected","selected");
			$("[name=saAreaId] option:first").attr("value", '00');
	}
	function setShow(){
		$("#serviceArea").attr("class","");
		$("#selects").attr("class","");
		
		$("[name=saProvinceId] option:first").prop("selected","selected");
		$("[name=saProvinceId] option:first").attr("value","");
		
		$("[name=saCityId] option:first").prop("selected", 'selected');
		$("[name=saCityId] option:first").attr("value", '');
		
		$("[name=saAreaId] option:first").prop("selected","selected");
		$("[name=saAreaId] option:first").attr("value", '');
	}
	//选择等级
	$("[name=shopsLevel]").live('change',function(){
		var shopsLevel = $(this).find("option:selected").text();
		var provinceId = $("[name=provinceId]").val();
		if(shopsLevel=='B级'){
			if(provinceId.length>0){
				//不能勾选服务门店
				$("[name=serviceShops]").removeAttr("checked");
				$("[name=serviceShops]").attr({disabled:'disabled'});
				//去掉服务区域
				setHidden();
				var content = "<tr id='shopsLevelChirld'><th><span class='requiredField'>*</span>请选择A级门店:</th><td>"
							  +"<select class='option' name='shopsLevelA'><option value=''>--请选择--</option></select></td></tr>";
				$("#shopsLV").after(content);
				$.ajax({
				    url:"/admin/shops/getShopsAList.jhtml",    //请求的url地址
				    dataType:"json",   //返回格式为json
				    async:true,//请求是否异步，默认为异步，这也是ajax重要特性
				    data:{"provinceId":provinceId},    //参数值
				    type:"GET",   //请求方式
				    success:function(req){
				    	for(var i=0;i<req.length;i++){
					    	$("[name=shopsLevelA]").append("<option value='"+req[i].id+"'>"+req[i].shopsName+"</option></select></td>");
				    	}
				    },
				    error:function(){
				        //请求出错处理
				        $.message("error","系统繁忙！");
				    }
				});
			}else{
				$.message("error","请先选择门店区域！");
				//默认选择请选择
				$("[name=shopsLevel] option:first").prop("selected","selected");
			}
			
		}else{
			var serviceShops = $("[name=serviceShops]").attr("checked");
			var orderShops = $("[name=orderShops]").attr("checked");
			if(serviceShops!="checked" && orderShops!="checked"){
				$("[name=tempShopsNature]").attr("value","");
			}else{
				$("[name=tempShopsNature]").attr("value","0");
			}
			if(serviceShops=="checked"){
				setShow();
			}else{
				setHidden();
			}
			$("[name=serviceShops]").removeAttr("disabled");
			$("#shopsLevelChirld").remove();
		}
	});
});
</script>
</html>